package cn.exrick.xboot.modules.your.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.your.entity.Region;
import cn.exrick.xboot.modules.your.service.IRegionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author chenyunxuan
 */
@Slf4j
@RestController
@Api(description = "陪护床管理接口")
@RequestMapping("/xboot/region")
@Transactional
public class RegionController {

    @Autowired
    private IRegionService iRegionService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Region> get(@PathVariable String id) {

        Region region = iRegionService.getById(id);
        return new ResultUtil<Region>().setData(region);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Region>> getAll(@ModelAttribute Region ere) {

        List<Region> list = iRegionService.list();
        return new ResultUtil<List<Region>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Region>> getByPage(@ModelAttribute PageVo page) {

        IPage<Region> data = iRegionService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Region>>().setData(data);
    }

    public static java.sql.Time strToTime(String strDate) {
        String str = strDate;
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        java.util.Date d = null;
        try {
            d = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.sql.Time time = new java.sql.Time(d.getTime());
        return time;
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Region> saveOrUpdate(@ModelAttribute Region region) {
        if (null != region.getBeginTimeString() && !"".equals(region.getBeginTimeString())) {

            region.setBeginTime(strToTime(region.getBeginTimeString()));
        }
        if (null != region.getEndTimeString() && !"".equals(region.getEndTimeString())) {

            region.setEndTime(strToTime(region.getEndTimeString()));
        }

        if (iRegionService.saveOrUpdate(region)) {
            return new ResultUtil<Region>().setData(region);
        }
        return new ResultUtil<Region>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids) {

        for (String id : ids) {
            iRegionService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
